package com.hundsun.cprs.yyt.service.common.service.impl;

import com.hundsun.cprs.shop.exporter.enums.EnumIntegralTransType;
import com.hundsun.cprs.shop.exporter.request.IntegralRequest;
import com.hundsun.cprs.shop.exporter.response.IntegralResponse;
import com.hundsun.cprs.shop.exporter.service.RemoteIntegralService;
import com.hundsun.cprs.yyt.common.base.BaseService;
import com.hundsun.cprs.yyt.common.exception.ServiceCodeException;
import com.hundsun.cprs.yyt.service.common.dto.IntegralInfo;
import com.hundsun.cprs.yyt.service.common.service.RemoteShopService;
import com.hundsun.cprs.yyt.util.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RemoteShopServiceImpl extends BaseService implements RemoteShopService {

    @Autowired(required=false)
    private RemoteIntegralService remoteIntegralService;

    @Override
    public void changeIntegral(IntegralInfo info) {
        IntegralRequest request = BeanUtils.transfrom(IntegralRequest.class,info);
        request.setType(EnumIntegralTransType.TRADE_REWARD.getCode());
        try{
            IntegralResponse response = remoteIntegralService.changeIntegral(request);
            if (!response.isSuccess()){
                throw new ServiceCodeException(response.getErrorNo(),response.getErrorInfo());
            }
        }catch (Exception e){
            logger.error("积分变更出错：userId：" + info.getUserId()+",变更数量："+info.getAmount()+",变更类型："+request.getType());
            throw e;
        }
    }
}
